<template>
  <div class="exam">
    <div class="main">
      <div class="header-wrapper">
        <div class="inner">
          <el-row>
            <el-col :span="18">
              <div class="grid-content bg-purple ovf left">
                <div class="logo">
                  <router-link to="/">
                    <img src="../../assets/images/logo.png" />
                  </router-link>
                </div>
                <div class="index"></div>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple right ovf">
                <div class="esc" @click="esc">退出</div>
                <div class="name">{{ name }}</div>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="mian-body">
        <div class="main-side">
          <div class="title">
            <div class="title_border"></div>
            <div class="title_content">答题卡</div>
          </div>
          <div class="card-content-list">
            <div class="card-content">
              <div class="card-content-title">
                共{{ examinationData.length }}题
              </div>
              <div class="box-list">
                <div
                  class="box normal-box question_cbox"
                  v-for="(item, index) in examinationData"
                  :key="index"
                >
                  <div
                    class="checki"
                    :class="{ checked: answersData[index] }"
                    v-show="!checkResult"
                  >
                    {{ index + 1 }}
                  </div>
                  <div
                    class="checki checked"
                    :class="{ 'check-error': answersData[index] }"
                    v-show="checkResult"
                  >
                    {{ index + 1 }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="main-center">
          <div class="body-wrapper">
            <div class="questions">
              <div class="questions-title">{{ title }}</div>
              <div class="questions-content">
                <div
                  class="question-content"
                  v-for="(item, i) in examinationData"
                  :key="i"
                >
                  <!--radiogroup单选-->
                  <div v-if="item.type == 1">
                    <div class="exam-question">
                      <span class="question-index ellipsis">{{ i + 1 }}.</span>
                      {{ item.title }}
                    </div>
                    <div v-if="checking == false">
                      <div
                        class="answers"
                        v-for="(son, index) in item.options"
                        :key="index"
                      >
                        <el-radio-group v-model="cur[i]">
                          <el-radio
                            :label="son.text"
                            @change="getInputValue(i)"
                            >{{ son.text }}</el-radio
                          >
                        </el-radio-group>
                      </div>
                    </div>
                    <div v-else>
                      <div
                        class="answers"
                        v-for="(son, index) in item.options"
                        :key="index"
                      >
                        <el-radio-group
                          :value="
                            examinationData[i].options[answersData[i]].text
                          "
                        >
                          <el-radio :label="son.text">{{ son.text }}</el-radio>
                        </el-radio-group>
                      </div>
                    </div>
                  </div>

                  <!--checkbox多选-->
                  <div v-if="item.type == 2">
                    <div class="exam-question">
                      <span class="question-index ellipsis">{{ i + 1 }}.</span>
                      {{ item.title }}
                    </div>

                    <div v-if="checking == false">
                      <div class="answers">
                        <el-checkbox-group v-model="cur[i]">
                          <el-checkbox
                            v-for="(son, index) in item.options"
                            :key="index"
                            :label="son.text"
                            @change="getInputValue(i)"
                          >
                            {{ son.text }}
                          </el-checkbox>
                        </el-checkbox-group>
                      </div>
                    </div>
                    <div v-else>
                      <div class="answers">
                        <el-checkbox-group :value="answersData[i]">
                          <el-checkbox
                            v-for="(son, index) in item.options"
                            :key="index"
                            :label="son.text"
                          >
                            {{ son.text }}
                          </el-checkbox>
                        </el-checkbox-group>
                      </div>
                    </div>
                  </div>

                  <!--textarea文本题-->
                  <div v-if="item.type == 3">
                    <div class="exam-question">
                      <span class="question-index ellipsis">{{ i + 1 }}.</span>
                      {{ item.title }}
                    </div>
                    <div v-if="checking == false">
                      <div class="answers">
                        <el-input
                          type="textarea"
                          autosize
                          placeholder="请输入内容"
                          v-model="cur[i]"
                          maxlength="200"
                          show-word-limit
                          @input="getInputValue(i)"
                        >
                        </el-input>
                        <div style="margin: 20px 0"></div>
                      </div>
                    </div>
                    <div v-else>
                      <div class="answers">
                        <el-input
                          type="textarea"
                          autosize
                          :value="answersData[i]"
                          maxlength="200"
                          show-word-limit
                          disabled
                        >
                        </el-input>
                        <div style="margin: 20px 0"></div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="main-right">
          <div class="nav">
            <ul v-show="!checkResult">
              <li class="menu-item">
                <div class="item-label">当前进度</div>
                <div class="item-press">
                  <span>{{ answersData.filter((v) => v).length }}</span>
                  <span>{{ examinationData.length }}</span>
                </div>
                <div class="percentage">
                  <el-progress
                    :percentage="percentage"
                    :color="customColor"
                  ></el-progress>
                </div>
              </li>
            </ul>
          </div>
          <div class="btn" v-show="!checkResult" @click="submit">提交</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      current: false,
      qid: 0,
      checking: false, //是否已经交卷
      name: "",
      title: "这是一张问卷",
      customColor: "#55b6da",
      percentage: 0,
      // submitView: false,
      // signView: false, // 正确与错误
      checkResult: null, // 左侧栏、右侧栏、答题结果栏
      examinationData: [
        {
          title: "",
          type: 1,
          must: true,
          options: [],
          index: 0,
        },
      ], //问卷题目，数据结构见获取问卷方法
      answersData: [], //提交给后台的题目答案
      cur: [], //临时题目答案,
    };
  },

  methods: {
    // 退出考试系统
    esc() {
      
        // 中途退出提示
        this.$confirm("是否退出", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            document.querySelector("html").style.cssText = `background: #fff;`;
            this.$router.push({
              path: this.checking == false ? "/unanswered" : "/answered",
            });
          })
          .catch();
    },

    // 提交答案接口
    async upAnswer(para) {
      const { data: res } = await this.$http.post("answerQuestionnaire", para);
      if (res.code == "666") {
        document.querySelector("html").style.cssText = `background: #fff;`;
        // 中途退出提示
        this.$confirm("保存成功", "提示", {
          confirmButtonText: "确定",
          type: "warning",
        })
          .then(() => {
            this.$router.push({
              path: this.current ? "/answered" : "/unanswered",
            });
          })
          .catch();
      } else {
        alert("提交失败");
      }
    },

    // 获取考试题
    get() {
      console.log(this.$route.query);
      this.name = this.$route.query.name;
      this.qid = this.$route.query.qid;
      this.title = this.$route.query.title;

      let check = this.$route.query.checking;
      console.log(check);
      if (check == "true"){
        this.checking = true;
        this.current = true;
      }
      else this.checking = false;
      console.log(this.checking);

      let content = this.$route.query.content;
      this.examinationData = JSON.parse(content);
      // console.log(this.examinationData)

      if (this.checking == true) {
        let answer = this.$route.query.answer;
        this.answersData = JSON.parse(answer);
        console.log(this.answersData);
        for (let i in this.answersData) {
          // if (this.examinationData[i].type == 1) {
          //   this.answersData[i] =
          //     this.examinationData[i].options[this.answersData[i]].text;
          // } else
          if (this.examinationData[i].type == 2) {
            for (let index in this.answersData[i]) {
              this.answersData[i][index] =
                this.examinationData[i].options[
                  this.answersData[i][index]
                ].text;
            }

            console.log(this.answersData[i].value);
          }
        }
        console.log(this.answersData);
      }

      let len =
        (this.answersData.filter((v) => v).length /
          this.examinationData.length) *
        100;
      this.percentage = len;
    },

    // 选择操作
    getInputValue(i) {
      this.answersData[i] = this.cur[i]; // 将数据存入提交给后台的数据中

      //计算当前进度
      let len =
        (this.answersData.filter((v) => v).length /
          this.examinationData.length) *
        100;
      this.percentage = len;
    },

    // 主动交卷
    submit() {
      console.log(this.answersData);

      this.$confirm("确定交卷吗?", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "交卷",
        cancelButtonText: "不交卷",
        type: "warning",
      })
        .then(() => {
          if (this.checking == false) {
            this.checking = true;

            for (let i in this.examinationData) {
              if (
                this.examinationData[i].type == 2 &&
                this.answersData[i] != null
              ) {
                let tmp = [];
                for (let key in this.examinationData[i].options) {
                  console.log(this.answersData[i]);
                  for (let index in this.answersData[i]) {
                    console.log("!!");
                    console.log(this.answersData[i][index]);
                    if (
                      this.answersData[i][index] ==
                      this.examinationData[i].options[key].text
                    ) {
                      tmp.push(key);
                      break;
                    }
                  }
                }

                this.answersData[i] = tmp;
              } else if (
                this.examinationData[i].type == 1 &&
                this.answersData[i] != null
              ) {
                for (let key in this.examinationData[i].options) {
                  if (
                    this.examinationData[i].options[key].text ==
                    this.answersData[i]
                  ) {
                    this.answersData[i] = key;
                    break;
                  }
                }
              }
            }
            console.log(this.answersData);

            let ans = {
              qid: this.qid,
              name: this.name,
              answer: JSON.stringify(this.answersData),
            };
            this.upAnswer(ans); //提交答案
          }
          // else {
          //   // this.upAnswer(1, JSON.stringify(this.answersData)); //提交答案
          // }
          // this.submitView = true; // 提示已提交
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },

  /**———————————————————— 我觉得以下代码没卵用，但是似乎又有点作用，所以暂时留着没有删除 ————————————————————*/
  beforeRouteEnter(to, from, next) {
    next(() => {
      // 改变html背景
      document.querySelector("html").style.cssText = `
        background: #ecf1f6;
      `;
    });
    // console.log(this)
  },
  created() {
    this.get();

    for (let i in this.examinationData) {
      if (this.examinationData[i].type == 2) {
        this.$set(this.cur, i, []);
      }
    }
  },
};
</script>


<style scoped lang="scss">
/deep/ .el-progress__text {
  display: none;
}
/deep/ .el-select .el-input__inner:focus {
  border-color: #55b6da;
}
/deep/ .el-select {
  border: none;
}
/deep/ .el-select-dropdown__item.selected {
  color: #55b6da !important;
  font-weight: bold;
}
/deep/ .el-radio__input.is-checked + .el-radio__label {
  color: #55b6da;
}
/deep/ .el-radio__input.is-checked .el-radio__inner {
  border-color: #55b6da;
  background: #55b6da;
}
/deep/ .el-radio__inner:hover {
  border-color: #55b6da;
}
/deep/ .el-radio {
  display: block;
  margin: 20px 0;
}
.inner {
  padding: 0px 90px;
}
.exam {
  background: #ecf1f6;
  height: 100%;
  min-height: 100%;
}
.main {
  .header-wrapper {
    height: 80px;
    width: 100%;
    line-height: 80px;
    background: #ffffff;
    box-shadow: 0 2px 4px 0 rgba(153, 153, 153, 0.1);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1040;
    .left {
      display: flex;
      align-items: center;
      font-size: 18px;
      .logo {
        width: 150px;
        margin-right: 120px;
        img {
          width: 100%;
        }
      }
    }
    .right {
      div {
        float: right;
        font-size: 16px;
      }
      .esc {
        width: 120px;
        height: 38px;
        line-height: 38px;
        margin: 21px 0;
        color: #fff;
        background: #55b6da;
        border-radius: 30px;
        text-align: center;
        cursor: pointer;
        user-select: none;
        &:hover {
          filter: brightness(80%);
        }
        &:active {
          filter: brightness(60%);
        }
      }
      .name {
        margin-right: 55px;
        font-weight: bold;
      }
    }
  }
  .mian-body {
    .main-side {
      display: inline-block;
      height: calc(100% - 140px);
      position: fixed;
      top: 120px;
      width: 240px;
      left: 90px;
      background: #fff;
      box-shadow: 0 1px 4px 0 rgba(58, 62, 81, 0.1);
      .title {
        position: relative;
        text-align: left;
        margin: 23px 0px 0px 12px;
        .title_border {
          display: inline-block;
          width: 4px;
          height: 26px;
          background: #33394b;
          border-radius: 15px;
          position: absolute;
          top: 0;
          bottom: 0;
          margin: auto;
        }
        .title_content {
          margin-left: 14px;
          font-size: 18px;
          font-weight: 600;
          color: #27274a;
        }
      }
      .card-content-list {
        height: calc(100% - 38px);
        overflow: auto;
        .card-content {
          padding: 0 12px 0 12px;
          position: relative;
          .card-content-title {
            font-size: 14px;
            color: #27274a;
            font-weight: 600;
            padding-bottom: 12px;
            padding-top: 20px;
          }
          .box-list {
            padding-bottom: 0;
            position: relative;
            left: -5px;
            font-size: 0;
            margin-right: -15px;
            .box {
              height: auto;
              line-height: unset;
              position: relative;
              margin-bottom: 15px;
              font-size: 14px;
              width: 35px;
              margin-top: unset;
              margin-right: unset;
              display: inline-block;
              .checki {
                border: 1px solid #dcdfe6;
                color: #dcdfe6;
                width: 27px;
                height: 27px;
                text-align: center;
                display: inline-block;
                line-height: 27px;
                background: #fff;
                border-radius: 50%;
                cursor: pointer;
              }
              .checked {
                color: #fff;
                background: #55b6da;
              }
              .check-error {
                color: #fff;
                background: #ec6941;
              }
            }
          }
        }
      }
    }
    .main-center {
      margin: 120px 230px 0px 360px;
      height: 100vh;
      .body-wrapper {
        color: #27274a;
        width: 100%;
        background: #ffffff;
        border: 1px solid #dedede;
        border-radius: 4px;
        .questions-title {
          font-size: 18px;
          line-height: 25px;
          padding: 18px 20px;
          background: #fafafa;
          border-bottom: 1px solid #dedede;
        }
        .questions-content {
          padding-left: 30px;
          padding-right: 75px;
        }
        .question-content {
          border-bottom: 1px solid #dedede;
          padding: 30px 0;
          position: relative;
          &:last-child {
            border: none;
          }
          .exam-question {
            font-size: 16px;
            line-height: 22px;
            margin-bottom: 10px;
            padding-left: 20px;
            position: relative;
            .question-index {
              color: #55b6da;
              position: absolute;
              left: -25px;
              top: 0;
              display: inline-block;
              width: 40px;
              line-height: 22px;
              text-align: right;
            }
          }
          .analysis {
            overflow: auto;
            background: rgba(222, 222, 222, 0.2);
            border-radius: 4px;
            padding: 15px 20px;
            line-height: 24px;
            margin-top: 10px;
            position: relative;
            .question-icon-wrapper {
              position: absolute;
              right: 10px;
              top: 14px;
              .sign {
                width: 48px;
                height: 28px;
                position: absolute;
                color: #fff;
                top: 10px;
                right: 0;
                font-size: 14px;
                border-radius: 2px;
                line-height: 28px;
                text-align: center;
                font-style: normal;
              }
              .icon-error {
                background: #ec6941;
              }
              .icon-right {
                background: #55b6da;
              }
            }
            .analysis-row {
              font-size: 14px;
              margin-top: 10px;
              min-height: 24px;
              padding-left: 80px;
              padding-right: 60px;
              position: relative;
              .analysis-title {
                position: absolute;
                width: 80px;
                left: 0;
                top: 0;
              }
              .question-analysis {
                text-align: justify;
              }
              .error {
                color: #f72a3a;
                font-weight: bold;
              }
            }
          }
        }
      }
    }
    .main-right {
      right: 90px;
      bottom: 20px;
      position: fixed;
      top: 120px;
      width: 120px;
      .nav {
        color: #27274a;
        line-height: 20px;
        padding: -20 10px;
        background: #ffffff;
        box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        margin-bottom: 10px;
        text-align: center;
        .menu-item {
          padding: 14px 0;
          border-bottom: 1px solid #dedede;
        }
        .menu-item:last-child {
          border: none;
        }
        .pass {
          color: #55b6da;
        }
        .item-result,
        .unpass {
          color: rgb(236, 105, 65);
        }
        .item-label {
          margin-bottom: 6px;
        }
        .item-data {
          font-size: 18px;
          line-height: 22px;
          color: #ff0000;
          font-weight: 400;
        }
        .item-press {
          line-height: 17px;
          margin-bottom: 7px;
          color: #27274a;
          font-size: 14px;
          & span:nth-child(1)::after {
            content: "/";
            margin: 0 5px;
          }
        }
      }
      .btn {
        bottom: -10px;
        position: absolute;
        width: 100%;
        margin-bottom: 10px;
        cursor: pointer;
        text-align: center;
        height: 38px;
        line-height: 38px;
        border-radius: 20px;
        color: #fff;
        background: #33394b;
        user-select: none;
        &:hover {
          filter: brightness(120%);
        }
        &:active {
          filter: brightness(60%);
        }
      }
    }
  }
}
</style>